define(['jquery','jquery_route','arttemplateUtil','common','login','config','validform'], function($,jquery_route,arttemplate,common,login,config) {
	var app = {
			init:function(){
				app.auth_init();
				// 初始化模块
				app.init_model();
				
				// 获取用户信息
				app.getsetuserinfo();
				
				$.controller("mainController",function($model,$tplHtml,$defobj,$modelobj,$paramobj,$this){
					// 参数 $this 如果是路由请求控制器那么会为空 否则是html对象
					//必须执行不然数据没法到页面
					$model($paramobj);
//					console.log('mainController');
				});
					
				$.controller("default", function($model, $tplHtml, $defobj,
						$modelobj, $paramobj, $this) {
					if(window.GLOBAL._historyrecord && window.GLOBAL._historyrecord.length>1){
						location.hash='#'+window.GLOBAL._historyrecord[1];
						window.$outmsg="没有找到指定路径:"+window.GLOBAL._historyrecord[0];
						return;
					}
					location.hash='#home/';
				});
				


				//$(this).on('click',function(e){
				// 住址超链接触发默认事件
				//	e.preventDefault();
				//});
				
				//路由设置
				$.routeBefore(function($hashstr){
					// 路由前置
					var _index1 = $hashstr.indexOf('/');
					if(_index1 != -1){
						var _has1=$hashstr.substring(0,_index1);
						var _node1=$('.fam_topMenu a[href="'+_has1+'"]');
						//设置一级选中 _node1
						// 1 清除样式
						// 2 设置选中项样式
						$('.fam_topMenu li').removeClass('active');
						_node1.parent('li').addClass('active');
						
						var _node2;
						var _has2 = $hashstr;
						while(true){
							//console.log(_has2)
							_node2 = $('.sectionDiv .sidebar-menu a[href="'+_has2+'"]');
							if(_node2.length > 0){
								break;
							}

							var _lasindex=_has2.lastIndexOf('/');
							if(_lasindex == -1){
								break;
							}
							_has2 = _has2.substring(0,_lasindex)
						}
						//设置二级选中 _node2
						// 1 清除样式
						// 2 设置选中项样式
						if(_node2.length > 0){
							$('.fam_section_menu').hide();
							var _aside = _node2.parents('.fam_section_menu');
							var _title = _node2.clone();
							_title.find('i').remove();
							$('.page-heading h3').text(_title.text());
							_aside.show();
							_aside.find('li').removeClass('active');
							_node2.parents('li').addClass('active');
						}

					}

				}).routeFn(function($tplHtml,$tplData,$objfn,$retobj){
					// 主方法 用于设置右侧内容
					if(window.layer){
						window.layer.closeAll();
					}
					if( typeof autojumptimeout == 'number'){
						clearTimeout(autojumptimeout);
					}
					if(typeof $objfn.noart === 'boolean' && $objfn.noart){
						$('#content-main').html($tplHtml);
					}else{
						$('#content-main').html(arttemplate.template.compile($tplHtml)($tplData));
					}
					
					if(typeof window.$outmsg =='string'){
						window.layer.msg(window.$outmsg);
						window.$outmsg=undefined;
					}
					
				}).route({
					//路由控制表
					// 1. 首页
					"home/":{templateUrl:"right.html"},
					"home":{redirectTo:"home/"},
					// 2. 生产管理
					//2_1 分润管理
					"shareprofit/merchant/getlist":{templateUrl:"customer/customer_list.html"},
					"shareprofit/merchantroute/getlist":{templateUrl:"customer/customer_edit.html"},

					"shareprofit/mechanism/getlist":{templateUrl:"plantplan/plantplan_list.html"},

					"shareprofit/mechanismroute/getlist":{templateUrl:"productplan/productplan_list.html"},
					"shareprofit/receivable/getlist":{templateUrl:"productplan/productplan_list.html"},
					"shareprofit/copewith/getlist":{templateUrl:"productplan/productplan_list.html"},
					"shareprofit":{redirectTo:"shareprofit/merchant/getlist"},
					//2_2对账管理
					"reconciliation/merchant/getlist":{templateUrl:"budget/budget.html"},
					"reconciliation/mechanism/getlist":{templateUrl:"budget/budget_list.html"},
					"reconciliation":{redirectTo:"reconciliation/merchant/getlist"},
				
					//2_3_1报表管理
					"reportform/accounted0/getlist":{templateUrl:"user/user_list.html"},
					"reportform/accounted1/getlist":{templateUrl:"user/user_edit.html"},
					"reportform/pay/getlist":{templateUrl:"user/user_list.html"},
					"reportform/liquidation/getlist":{templateUrl:"user/user_edit.html"},
					"reportform/route/getlist":{templateUrl:"user/user_edit.html"},
					"reportform":{redirectTo:"reportform/accounted0/getlist"},
					//2_3_2商户管理
					"merchant/merchant/getlist":{templateUrl:"merchant/merchant_list.html"},
					"merchant/paychannel/getlist":{templateUrl:"merchant/userpaychannel_list.html"},
					"merchant/bankcardauth/getlist":{templateUrl:"role/role_list.html"},
					"merchant/livelihoodchannel/getlist":{templateUrl:"role/role_edit.html"},
					"merchant":{redirectTo:"merchant/merchant/getlist"},
					//2_3_3订单管理
					"order/convenience/getlist":{templateUrl:"setdata/datamanage/varietylibrary.html"},
					"order/liquidation/getlist":{templateUrl:"setdata/datamanage/mater.html"},
					"order/pay/getlist":{templateUrl:"setdata/datamanage/template.html"},
					"order/transaction/getlist":{templateUrl:"setdata/manageland/index.html"},
					"order":{redirectTo:"order/transaction/getlist"},
					//2_3_4风控管理
					"risk/merchantaccounted/getlist":{templateUrl:"setdata/datamanage/varietylibrary.html"},
					"risk/identity/getlist":{templateUrl:"setdata/datamanage/mater.html"},
					"risk/bank/getlist":{templateUrl:"setdata/datamanage/template.html"},
					"risk/settlementperson/getlist":{templateUrl:"setdata/manageland/index.html"},
					"risk/settlement/getlist":{templateUrl:"setdata/manageland/index.html"},
					"risk/settlementgrey/getlist":{templateUrl:"setdata/manageland/index.html"},
					"risk/settlementblack/getlist":{templateUrl:"setdata/manageland/index.html"},
					"risk/repay/getlist":{templateUrl:"setdata/manageland/index.html"},
					"risk":{redirectTo:"risk/merchantaccounted/getlist"},
					//2_3_5系统管理
					"system/route/getlist":{templateUrl:"setdata/datamanage/varietylibrary.html"},
					"system/errorcode/getlist":{templateUrl:"setdata/datamanage/mater.html"},
					"system/errorlog/getlist":{templateUrl:"setdata/datamanage/template.html"},
					"system/model/getlist":{templateUrl:"setdata/manageland/index.html"},
					"system/operatelog/getlist":{templateUrl:"setdata/manageland/index.html"},
					"system/user/getlist/{type}":{templateUrl:"user/user_list.html"},
					"system/user/userEdit/{id}":{templateUrl:"user/user_edit.html"},
					"system/role/getlist":{templateUrl:"role/role_list.html"},
					"system/role/roleAdd/{id}":{templateUrl:"role/role_edit.html"},
					"system/holiday/getlist":{templateUrl:"setdata/manageland/index.html"},
					"system/secretkey/getlist":{templateUrl:"setdata/manageland/index.html"},
					"system/bankcard/getlist":{templateUrl:"setdata/manageland/index.html"},
					"system":{redirectTo:"system/route/getlist"},
					//3.溯源管理
					//"trace":{redirectTo:"trace/home"},

					//4. DEMO
					"demo/image_upload_edit":{templateUrl:"demo/image_upload_edit.html"},
					"demo/ztree_edit":{templateUrl:"demo/ztree_edit.html"},		
					"demo/city_edit":{templateUrl:"demo/city_edit.html"},	
					"product/set/demolist":{templateUrl:"demo/demos_list.html"},
					"demo/multObjSubmit":{templateUrl:"demo/city_edit.html"},
					
					
					// 默认跳转
					"(.*?)":{controller:'default'}
				});
			},
			init_model:function(){
				// art 页面模块定义
				$.artmodel({
					"goods.class":{templateUrl:"goods/goodsclass.html"},
					"goods.category":{templateUrl:"goods/goodscategory.html"},
					"goods.skulist":{templateUrl:"goods/skulist.html"},
					"goods.vip_trans_area_list":{templateUrl:"goods/vip_trans_area_list.html"},
					"goods.store_frequency_list":{templateUrl:"goods/store_frequency_list.html"}
				});
			},
			// 获取设置用户信息
			getsetuserinfo:function(){
				common.ajax_request2('get','/api/v1/queryUserInfo',{},function($data){
					if(typeof $data.nick_name === 'string' && $data.nick_name != ''){
						$('.dropdown-toggle span:first').html($data.nick_name);
					}else{
						$('.dropdown-toggle span:first').html($data.userName);
					}
				});
			},
			// 初始化
			auth_init:function(){
				
				//修改密码弹出框
				$('.dropdown-menu').on('click','a:first',function(e){
					e.preventDefault();
					$('#hangepwdmodal form')[0].reset();
					$('#hangepwdmodal .errinfo').removeClass('Validform_right').removeClass('Validform_wrong').html('');
					$('#hangepwdmodal').modal('show');
				});
				
				//个人信息弹出框
				$('.dropdown-menu').on('click','#userDetail',function(e){
					$('#userdetailmodal').modal('show');
					common.ajax_request2('get','/api/v1/getUserDetail',{},function($data){			
						$("#login_user_name").val($data.user_name);
						$('#login_orga_name').val($data.orga_name);
						$('#login_role_group_name').val($data.role_group_name);
						$('#login_ro_name').val($data.ro_name);
						if($data.user_type=='1'){
							$('#login_user_type').val("总部员工");
						}else if($data.user_type=='2'){
							$('#login_user_type').val("农企负责人");
						}else if($data.user_type=='21'){
							$('#login_user_type').val("农企");
						}
					});
				});
				
				
				//修改密码提交
				$('#hangepwdmodal').on('click','.btn-primary',function(e){
					e.preventDefault();
					var hangepwdfrm=$("#hangepwdmodal form").Validform({
						tiptype:3,
						isShowRight:false
					});
					if(hangepwdfrm.check()){
						changepwd($('#hangepwdmodal form').serializeArray(),function($data){
							if($data.code=='200'){
								$('#hangepwdmodal form')[0].reset();
								$('#hangepwdmodal .errinfo').removeClass('Validform_wrong').addClass('Validform_right').html('密码修改成功!请妥善保管你的密码!');
								login.loginOut();
							}else{
								if($data.businessMsg){
									$('#hangepwdmodal .errinfo').removeClass('Validform_right').addClass('Validform_wrong').html($data.businessMsg.businessNote);
								}else{
									$('#hangepwdmodal .errinfo').removeClass('Validform_right').addClass('Validform_wrong').html('密码修改失败!');
								}
							}
						});
					}
					
				});
				
				// 退出
				$('.navbar-right .dropdown-menu').on('click','a:last',function(e){
					e.preventDefault();
					login.loginOut();
				});
				checkLogin();
				//window.setInterval(function(){
				//	checkLogin();
				//},10000);
			}
	};
	
	return {init:function(){return app.init();}}
});